/**
 * Created by wenchuan on 2018/5/10.
 */
define(function () {

    var self,
        app = win.app,
        detailData,
        characteristics = [],
        gwcxData = {
            GWCX005: '0',
            GWCX006: '0',
            GWCX007: '0',
            GWCX008: '0'
        },
        comments = [],
        //imgs = [],
        zlh = [], //所有的子料号
        other = {
            GWAM016: '',
            GWAM017: ''
        },
        refreshData = {
            key: 0, //评价key
            status: 0,
            count: 0,
            count1: 0,
            count2: 0,
            count3: 0,
            pv: 0
        },
        pageParameters
    ;

    var ClassesInfo = app.Module.extend({

        init: function () {
            self = this;
            self.data = {};
            pageParameters = app.pageParameters['classesinfo_page'];
            detailData = app.cloneJSON(pageParameters.order);
            detailData.GWBK001_ = detailData.GWBK001;// 料号
            detailData.GWBK002_ = detailData.GWBK002;// 单位
            detailData.GWBK004_ = Number(detailData.GWBK004);//单价
            detailData.GWAM015_ = detailData.GWAM015;
            detailData.GWBK008 = (detailData.GWBK008||'').replace(/\//g,'-');
            detailData.GWBK009 = (detailData.GWBK009||'').replace(/\//g,'-');

            detailData.imgs = detailData.imgs || [];
            if (!detailData.GWCQ060) {
                Vue.set(detailData, 'GWCQ060', '');
            }

            if (detailData.GWCQ061) {
                detailData.GWCQ061s = detailData.GWCQ061.split(',');
            } else {
                detailData.GWCQ061s = [];
            }

            self._super({}, win);// 放在后边
        },
        getGwcxData: function () {
            app.ajax({
                xurl: "api/wx/gwat300/getGwcxData",
                xtime: '1d',
                xdata: {
                    GWCX002: detailData.GWBK001_
                },
                cb: function (result) {
                    if (result.ok && result.data.data != null) {
                        Vue.set(gwcxData, 'GWCX005', result.data.data.GWCX005);
                        Vue.set(gwcxData, 'GWCX006', result.data.data.GWCX006);
                        Vue.set(gwcxData, 'GWCX007', result.data.data.GWCX007);
                        Vue.set(gwcxData, 'GWCX008', result.data.data.GWCX008);
                    }
                }
            });
        },
        getGwcwCount: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwcwCount",
                xtime: '1m',
                ocache: true,
                xdata: {
                    GWCW020: detailData.GWBK001_,
                    GWAM005: 1,
                    status: refreshData.status
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.count;
                        app.extend(refreshData, result.data.count);
                        if (data.count > 0) {
                            refreshData.pv = (refreshData.count1 / refreshData.count * 100).toFixed(2);
                        } else {
                            refreshData.pv = 0;
                        }
                    }
                }
            });
        },
        getGwamOther: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwamOther",
                xtime: '1d',
                xdata: {
                    GWAM001: detailData.GWBK001_
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        app.extend(other, data);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll3'];
                self._refreshVueData = refreshData;
                self._refreshVueMethods = {
                    statusChange: function () {
                        self._refresh();
                        self.getGwcwCount();
                    },
                    getpj: function (val) {
                        refreshData.key = val;
                        self._refresh();
                    }
                };
                this._super(win);
            } else {
                self._refresh();
            }
            self.getGwcwCount();
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcw",
                xtime: '1m',
                ocache: true,
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWCW020: detailData.GWBK001_,
                    key: refreshData.key,//评论级别
                    GWAM005: 1, //料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        _super({
                            obj: goods,
                            data: datas,
                            nodata: '暂无评论信息...', nodata_img: 'noneComment.png'
                        });
                    }

                }
            });
        },
        _lazy: function () {

            //self.getGwcxData();

            self.getGwamOther();

            $('.wf-infoTab ul li').on('tap', function () {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).parents('.wf-infoTab').find('.tabCont .group').eq($(this).index()).show().siblings('.group').hide();
            });

            doc.getElementById('slider').addEventListener('slide', function (e) {
                if (e.detail.slideNumber == 2) {
                    self._initRefresh(win);
                }
            });

            app.taps.call(win, [{
                o: 'btnSave1',
                cb: function (e) {
                    var validatorResult = app.validator2Json(detailData, {
                        GWAP100_1: {
                            type: 'number',
                            nul: [false, '实际价格不能为空']
                        },
                        GWBK008: {
                            nul: [false, '预计购买日期不能为空']
                        },
                        GWBK009: {
                            nul: [false, '预计回款日期不能为空']
                        }
                    });

                    if (validatorResult.ok) {
                        pageParameters.order.GWBK001 = detailData.GWBK001_;
                        pageParameters.order.GWBK002 = detailData.GWBK002_;
                        pageParameters.order.GWBK003 = detailData.GWBK003;
                        //detailData.GWBK004 = detailData.GWBK004_;
                        pageParameters.order.GWAM015 = detailData.GWAM015_;
                        pageParameters.order.GWBK004 = detailData.GWAP100_1;
                        pageParameters.order.GWBK008 = detailData.GWBK008;
                        pageParameters.order.GWBK009 = detailData.GWBK009;
                        app.back(1);
                    }
                },
                // confirm: {
                //     msg: '确定保存商品？'
                // }
            }]);
        },
        _ready: function () {
            var scroll1Vue = new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    item: detailData,
                    characteristics: characteristics,
                    gwcxData: gwcxData,
                    comments: comments,
                    imgs: detailData.imgs
                },
                watch: {
                    'item.GWBK008': function (newVal, oldVal) {
                        if (detailData.GWBK009 == '') {
                            detailData.GWBK009 = newVal;
                        }
                    }
                },
                methods: {
                    minus: function (val) {
                        if (val.GWBK003 > 1) {
                            val.GWBK003--;
                        }
                    },
                    plus: function (val) {
                        val.GWBK003++;
                    },
                    addImg: function () {
                        var _len = 6 - detailData.imgs.length;
                        if (_len > 0) {
                            app.chooseImageNew(function (imgs_) {
                                //detailData.imgs.pushArray(imgs_);
                                for (var i = 0; i < imgs_.length; i++) {
                                    detailData.imgs.push({
                                        img: imgs_[i].img,
                                        id: imgs_[i].localId
                                    })
                                }
                            }, _len);
                        }
                    },
                    scanCode: function () {
                        app.scanQRCode(function (s) {
                            detailData.GWCQ060 = s;
                        });
                    }
                }
            });
            app.getCharacteristic({
                gwan001_: detailData.GWBK001,
                gwam005_: 1,
                _vue: scroll1Vue,
                cb: function (result) {
                    //用到缓存 会调用2次，不要放跟特征值无关的逻辑

                },
                changeCb: function (obj) {
                    detailData.GWBK001_ = obj.GWAM001;
                    detailData.GWAML003 = obj.GWAML003;
                    detailData.GWBK002_ = obj.GWAP004;
                    detailData.GWBK004_ = Number(obj.GWAP100);
                    detailData.GWAP100_1 = detailData.GWAP100_1;
                    detailData.GWAM015_ = obj.GWAM015;
                    self.getGwcxData();
                    self.getGwamOther();
                }
            });
            Vue.component('commentsitem', {
                template: app.getByid(doc, 'comments-template'),
                props: {
                    model: Array
                }
            });
            new Vue({
                el: app.getByid(doc, 'scroll2'),
                data: other
            });

        }
    });

    new ClassesInfo();

});